package com.paomi.client.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.paomi.client.R;
import com.paomi.client.widget.click.SingleClick;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by LS on 2017/5/17 0017.
 *
 * 通用标题栏，固定预定义ID{@link R.id#title_view},如需使用{@link View#findViewById(int)}
 * ,请使用{@link R.id#title_view}
 */

public class CommonTitleView extends LinearLayout {

    @BindView(R.id.head_left)
    ImageView headLeft;
    @BindView(R.id.head_title)
    TextView headTitle;
    @BindView(R.id.head_right_text)
    TextView headRightText;
    @BindView(R.id.head_right_img)
    ImageView headRightImg;
    private String mTitle;
    private String mRightText;
    private Drawable mLeftDrawable;
    private Drawable mRightDrawable;

    public CommonTitleView(Context context) {
        this(context, null);
    }

    public CommonTitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CommonTitleView);

        mTitle = a.getString(R.styleable.CommonTitleView_title);
        mRightText = a.getString(R.styleable.CommonTitleView_rightText);
        for (int i = 0; i < a.length(); i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.CommonTitleView_leftDrawable:
                    mLeftDrawable = a.getDrawable(attr);
                    break;
                case R.styleable.CommonTitleView_rightDrawable:
                    mRightDrawable = a.getDrawable(attr);
                    break;
            }
        }
        // 如果没有设置title，则默认 小遛共享
        if (TextUtils.isEmpty(mTitle)) mTitle = context.getResources().getString(R.string.app_name);
        // 如果没有设置左边图片，默认 返回按钮
//        if(mLeftDrawable == null) mLeftDrawable = context.getResources().getDrawable(R.mipmap.icon_back);
        a.recycle();

        setId(R.id.title_view);
        Drawable background = getBackground();
        if(background == null) setBackgroundColor(context.getResources().getColor(R.color.text_title));
        initView();
    }

    private void initView() {
        View.inflate(getContext(), R.layout.layout_common_title, this);
        ButterKnife.bind(this, this);

        headLeft.setImageDrawable(mLeftDrawable);
        headTitle.setText(mTitle);
        if(mRightDrawable != null) {    // 有图片
            headRightImg.setVisibility(View.VISIBLE);
            headRightText.setVisibility(View.GONE);
            headRightImg.setImageDrawable(mRightDrawable);
        } else if(!TextUtils.isEmpty(mRightText)) { // 只有文字
            headRightImg.setVisibility(View.GONE);
            headRightText.setVisibility(View.VISIBLE);
            headRightText.setText(mRightText);
        } else {    // 图片文字都没有
            headRightImg.setVisibility(View.INVISIBLE);
            headRightText.setVisibility(View.GONE);
        }
    }

    public void setTitle(@StringRes int resId) {
        this.mTitle = getContext().getResources().getString(resId);
        headTitle.setText(mTitle);
    }

    public void setTitle(@NonNull String title) {
        this.mTitle = title;
        headTitle.setText(mTitle);
    }

    public void setRightDrawable(Drawable right) {
        if(right == null) {
            this.headRightImg.setVisibility(INVISIBLE);
            return;
        }
        this.mRightDrawable = right;
        this.headRightImg.setVisibility(VISIBLE);
        this.headRightText.setVisibility(GONE);
        this.headRightImg.setImageDrawable(mRightDrawable);
    }

    public void setLeftDrawable(Drawable left) {
        if(left == null) return;
        this.mLeftDrawable = left;
        this.headLeft.setImageDrawable(mLeftDrawable);
    }

    @SingleClick
    @OnClick({R.id.head_left, R.id.head_right_text, R.id.head_right_img, R.id.head_title})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.head_title:
                if(listener != null) listener.onTitleClick();
                break;
            case R.id.head_left:
                if(listener != null) listener.onLeftClick();
                break;
            case R.id.head_right_img:
            case R.id.head_right_text:
                if(listener != null) listener.onRightClick(view);
                break;
        }
    }

    private OnTitleItemClickListener listener;

    public void setOnTitleItemClickListener(OnTitleItemClickListener listener) {
        this.listener = listener;
    }

    /** 获取右边图片布局*/
    public View getRightView() {
        return this.headRightImg;
    }

    public interface OnTitleItemClickListener {
        void onLeftClick();

        void onRightClick(View view);

        void onTitleClick();
    }

    public static class SimpleOnTitleItemClickListener implements OnTitleItemClickListener {

        @Override
        public void onLeftClick() {

        }

        @Override
        public void onRightClick(View view) {

        }

        @Override
        public void onTitleClick() {

        }
    }
}
